<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT. 'util/dataspace.inc.php';
require_once APP_ROOT.'view/pub/layoutright.view.php';

require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/categories.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/article.php';
require_once APP_ROOT.'model/common/scrolling.php';
require_once APP_ROOT . "model/common/site.php";


class IndexView extends LayoutRightView {
	function IndexView ($TemplateFile){
		parent::LayoutRightView($TemplateFile);

	}
	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare();

		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
        $this->Template->set('title',Site::getTitle());
		if(!Session::isLogin())
			$this->Template->set('login',' ');

		if(APP_NAME!="") {
			$this->Template->set('app_name',APP_NAME);
		} else
			$this->Template->set('app_name','.');

        // AD BOXES
        $banner=&LogoBannersModel::getBanner('ROW1ADBOX1');
        $row1 = isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX11banner_link', $banner[2]);
        $this->Template->set('ADBOX11banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW1ADBOX2');
        $row1 = $row1 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX12banner_link', $banner[2]);
        $this->Template->set('ADBOX12banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW1ADBOX3');
        $row1 = $row1 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX13banner_link', $banner[2]);
        $this->Template->set('ADBOX13banner_alt', $banner[3]);
        if ($row1) $this->Template->set('ROW1', 1);


        $banner=&LogoBannersModel::getBanner('ROW2ADBOX1');
        $row2 = isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX21banner_link', $banner[2]);
        $this->Template->set('ADBOX21banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW2ADBOX2');
        $row2 = $row2 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX22banner_link', $banner[2]);
        $this->Template->set('ADBOX22banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW2ADBOX3');
        $row2 = $row2 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX23banner_link', $banner[2]);
        $this->Template->set('ADBOX23banner_alt', $banner[3]);
        if ($row2) $this->Template->set('ROW2', 1);

        $banner=&LogoBannersModel::getBanner('ROW3ADBOX1');
        $row3 = isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX31banner_link', $banner[2]);
        $this->Template->set('ADBOX31banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW3ADBOX2');
        $row3 = $row3 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX322banner_link', $banner[2]);
        $this->Template->set('ADBOX32banner_alt', $banner[3]);

        $banner=&LogoBannersModel::getBanner('ROW3ADBOX3');
        $row3 = $row3 || isset($banner[1]);
        $this->Template->set($banner[0], $banner[1]);
        $this->Template->set('ADBOX33banner_link', $banner[2]);
        $this->Template->set('ADBOX33banner_alt', $banner[3]);
        if ($row3) $this->Template->set('ROW3', 1);


		$news =& Scrolling::getRendomRecord(null,null);
		if($news) {
			$headline = $news['headline'];
			$headline = Article::getShortText2($headline,32);
			$this->Template->set('headline',$headline);
			$this->Template->set('image_url',$news['photo_url']);
			$this->Template->set('summary',$news['summary']);
			if(isset($news['orientation'])&&$news['orientation']==1)
				$this->Template->set('horizontal',' ');
		} else {
			$article=&Article::getRecordForScrolling(null,null);
			if($article){
                $picture_url = $article['front_picture'];
                $headline = $article['headline'];
				$headline = Article::getShortText2($headline,32);
				$this->Template->set('headline',$headline);
				$this->Template->set('image_url',$picture_url);

				$this->Template->set('summary',$article['summary']);
				$this->Template->set('article',' ');
				$this->Template->set('art_id',$article['article_id']);
				//print $url;
                $mypos = strpos($picture_url, '/'); // if starts with /, append .
				if ( $mypos === 0) $picture_url = '.'.$picture_url;
                //$picture_url = str_replace(' ','%20',$picture_url); //REPLACE THE SPACES
				$image_prop = @getimagesize($picture_url);
				$width = $image_prop[0];
				$height = $image_prop[1];
				//if ( $width>300)
				$width = 300;
				$this->Template->set('width',$width);
				//print $picture_url.' '.$width.' '.$height;
				//if($width>$height) $orientation='h';
				//else if($width<$height)$orientation='v';
				//else if($width==$height&&$width<=320) $orientation='v';
				//else $orientation='h';
				//if($orientation=='h')  {
					//$this->Template->set('horizontal',' ');
					//if($width>430) $this->Template->set('width','width=580');
				//}else if($width>240) $this->Template->set('width','width=340');

			}
		}

		$list_art=Article::getLatestArticles(null,null);
		$this->Template->setChildDataSource('latestheadlines',$list_art);

		$list_art2=Article::getLatestArticles2(null,null);
		$this->Template->setChildDataSource('latestarticles',$list_art2);

		$videoShow = Parameters::getVideoStatus();
		if ( $videoShow ) {
			// set video playlist XML file
        	$filename =& Video::getPlaylistFilename(0,0);
        	$this->Template->set('playlist',$filename);
        	Video::savePlaylistFile(0,0);

        	// prevent local caching
        	$rnumber = rand(1, 9999999);
        	$this->Template->set('rnumber',$rnumber);
        }

        // set video playlist XML file
        //$filename =& Video::getPlaylistFilename(0,0);
        //$this->Template->set('playlist',$filename);

        //Video::savePlaylistFile(0,0);

	}
}



class IndexPage extends PageController {

    function IndexPage() {
        parent::PageController();
        $site_template = Site::getPublicTemplatePath('/pub/index.html');

        $this->setDefaultView(new IndexView($site_template));
    }

}

?>
